package com.cjb.cloudsystem.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cjb.cloudsystem.entity.DeviceStatus;
import com.cjb.cloudsystem.service.DeviceStatusService;
import com.cjb.cloudsystem.vo.DeviceStatusVO;
import com.cjb.cloudsystem.vo.UsersExcelVO;
import com.cjb.common.common.cache.RedisCache;
import com.cjb.common.common.excel.ExcelUtil;
import com.cjb.common.exception.ServerException;
import com.cjb.common.util.Common;
import com.cjb.common.util.HttpContextUtils;
import com.cjb.common.util.JsonUtils;
import com.cjb.common.util.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
* @Author 1.0.0 2023-11-05
* @Package IntelliJ IDEA
* @Class DeviceStatusControl
* @Date 2023-11-05
*/
@RestController
@Tag(name="deviceStatus")
@RequestMapping(value = {"deviceStatus"})
@ApiResponses(@ApiResponse(responseCode = "200", description = "接口请求成功"))
@Slf4j
public class DeviceStatusController extends ExcelUtil{
    /*
    * 注入事物类
    */
    @Autowired
    private DeviceStatusService deviceStatusService;
    /**
     * 注入redis类
     */
    @Autowired
    private  RedisCache redisCache;
    /**
    *单个保存
    *
    * @Author 1.0.0 2023-11-05
    * @Value
    * @Title
    */
    @PostMapping("/save")
    @Operation(summary = "单个保存")

    public Result<Boolean> save(@RequestBody DeviceStatus deviceStatus){
        deviceStatus.setDeviceStatusId(null);
        return Result.success(deviceStatusService.save(deviceStatus));
    }

    /**
    *批量保存
    *
    * @Author 1.0.0 2023-11-05
    * @Value
    * @Title
    */
    @PostMapping("/listSave")
    @Operation(summary = "批量保存")
    public Result <Integer> listSave(@RequestBody List<DeviceStatus> deviceStatus){
        return Result.success(deviceStatusService.listSave(deviceStatus));
    }

    /**
    *单个查询
    *
    * @Author 1.0.0 2023-11-05
    * @Value
    * @Title
    */
    @PostMapping("/queryById")
    @Operation(summary = "单个查询-通过ID")
    public Result<DeviceStatus> queryById(@RequestBody DeviceStatus deviceStatus){
        log.info("打印收到的参数{}", JsonUtils.toJsonString(deviceStatus));
        if (Common.isNotEmpty(deviceStatus) && Common.isEmpty(deviceStatus.getDeviceStatusId())){
            throw  new ServerException("Id为空");
        }
        log.info("打印收到的参数",deviceStatus.getDeviceStatusId());
        return Result.success( deviceStatusService.queryById(deviceStatus.getDeviceStatusId()));
    }

    /**
    * 分页查询
    *
    * @Author 1.0.0 2023-11-05
    * @Value
    * @Title
    */
    @GetMapping("/queryPage")
    @Operation(summary = "分页查询-通过页码")
    public Result<Page<DeviceStatus>> queryPage(@ParameterObject @Valid DeviceStatusVO deviceStatusVO){
        log.info("查看查询参数 {}",JsonUtils.toJsonString(deviceStatusVO));
        Page deviceStatusPage = new Page<DeviceStatus>(deviceStatusVO.getPage(),deviceStatusVO.getLimit());
        return Result.success( deviceStatusService.queryPage(deviceStatusPage,deviceStatusVO));
    }
    /**
     *
     *
     * @Author chenjinbin
     * @Value
     * @Title
     */
    @PostMapping("/queryOneDay")
    @Operation(summary = "查询单个")
    public Result<List<DeviceStatus>> queryOneDay(@RequestBody DeviceStatus deviceStatus){
        return Result.success(deviceStatusService.queryList(deviceStatus));
    }
    /**
     * 分页查询
     *
     * @Author 1.0.0 2023-11-05
     * @Value
     * @Title
     */
    @GetMapping("/queryOne")
    @Operation(summary = "查询单个")
    public Result<DeviceStatus> queryList(@ParameterObject @Valid DeviceStatusVO deviceStatusVO){
        return Result.success((DeviceStatus)redisCache.hGet("message","deviceStatus"));
    }

    /**
    * 批量更新
    *
    * @Author 1.0.0 2023-11-05
    * @Value
    * @Title
    */
    @PostMapping("/listUpdate")
    @Operation(summary = "批量更新")
    public Result <String> listUpdate(@RequestBody List<DeviceStatus> deviceStatus){
        deviceStatusService.listUpdate(deviceStatus);
        return Result.success();
    }
    /**
     * 单个更新
     *
     * @Author chenjinbin
     * @Value
     * @Title
     */
    @PostMapping("/update")
    @Operation(summary = "批量更新")
    public Result <String> update(@RequestBody DeviceStatus deviceStatus){
        deviceStatusService.update(deviceStatus);
        return Result.success();
    }


    /**
    * 批量删除
    *
    * @Author 1.0.0 2023-11-05
    * @Value
    * @Title
    */
    @PostMapping("/listDelete")
    @Operation(summary = "批量删除")
    public Result <String> listDelete(@RequestBody List<DeviceStatus> deviceStatus){
        deviceStatusService.listDelete(deviceStatus);
        return Result.success();
    }
    /**
    * 通过id删除
    *
    * @Author 1.0.0 2023-11-05
    * @Value
    * @Title
    */
    @PostMapping("/deleteById")
    @Operation(summary = "单个删除-通过ID")
    public Result <String> deleteById(@RequestBody DeviceStatus deviceStatus){
        deviceStatusService.deleteById(deviceStatus);
        return Result.success();
    }

    /**
    * 返回全部信息
    *
     * @Author 1.0.0 2023-11-05
     * @Value
     * @Title
     */
    @PostMapping("/returnAllData")
    @Operation(summary = "返回所有信息")
    public Result<List<DeviceStatus>> returnAllData() {
        return Result.success(deviceStatusService.returnAllData());
    }




    @PostMapping("/test")
    @Operation(summary = "返回所有信息")
    public void test() {
//        //区块数据查询
//        String asset = null;
//        try {
//            asset = operateFabricService.QueryAllAsset();
//        } catch (Exception e) {
//            String errMsg = "查询出现错误" + e.getMessage();
//        }
//        //sql数据查询
//        List<DeviceStatus> deviceStatusList = deviceStatusService.returnAllData();
//
//        //数据归一 统计有意义的数据
//        if (Common.isNotEmptyVales(asset, deviceStatusList)) {
//            log.info("打印这两个数据");
//            List<BasicAsset> basicAsset = JsonUtils.parseArray(asset, BasicAsset.class);
//            log.info(JsonUtils.toJsonString(basicAsset));
//            log.info(JsonUtils.toJsonString(deviceStatusList));
//        }
//        return Result.success(true);
        //导出测试
        UsersExcelVO deviceStatus = new UsersExcelVO();
        List<UsersExcelVO> list = new ArrayList<>();
        deviceStatus.setGender(1);
        deviceStatus.setEmail("12324");
        list.add(deviceStatus);
        deviceStatus.setGender(0);
        this.exportToExcel(list, UsersExcelVO.class, HttpContextUtils.getHttpServletResponse(), "test","Sheet1");
        log.info("执行了导出操作");
    }

}
